package cn.edu.tbu.tydemo.service.impl;

import cn.edu.tbu.tydemo.domain.Student;
import cn.edu.tbu.tydemo.repository.StudentRepository;
import cn.edu.tbu.tydemo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentImplService implements StudentService{


    @Autowired
    StudentRepository studentRepository;

    /**
     * 全部学生
     * @return
     */
    public List<Student> findAll(){
        return studentRepository.findAll();
    }

    /**
     * 支持分页的查询
     * @param pageable
     * @return
     */
    public Page<Student> findAll(Pageable pageable){
        return studentRepository.findAll(pageable);
    }

    /**
     * 带过滤的分页查询
     * @param student
     * @param pageable
     * @return
     */
    public Page<Student> findAll(Example<Student> student , Pageable pageable){
        return studentRepository.findAll(student,pageable);
    }

    /**
     * 根据姓名查询
     */
    public List<Student> findByName(String name){
        return studentRepository.findByName(name);
    }

    /**
     * 根据id查找
     * @param id
     * @return
     */
    public Student findById(Integer id){
        return studentRepository.findById(id).orElse(null);
    }

    /**
     * 插入学生
     * @param student
     * @return
     */
    public Student insert(Student student){
        return studentRepository.save(student);
    }

    /**
     * 更新学生
     * @param student
     * @return
     */
    public Student update(Student student){
        return studentRepository.save(student);
    }

    /**
     * 删除学生
     * @param student
     */
    public void delete(Student student){
        studentRepository.delete(student);
    }

    /**
     * 根据id删除
     * @param id
     */
    public void delete(Integer id){
        Student student = new Student();
        student.setId(id);
        studentRepository.delete(student);
    }
}
